package com.unicom.projt.query;

import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-14
 * Time: 下午1:13
 */
public class QueryController extends AbstractHttpController<String> {
    private static final int QUERY_AB = 0;//可用余额
    private static final int QUERY_CM = 1;//当前月
    private static final int QUERY_HT = 2;//历史记录
    private static final int QUERY_PR = 3;//套餐余量
    private static final int QUERY_PM = 4;//缴费记录

    private QueryAvailableBalancePage queryABPage;
    private QueryCurrentMonthPage queryCMPage;
    private QueryHistoryPage queryHTPage;
    private QueryPackageRemainPage queryPRPage;
    private QueryPaymentPage queryPMPage;

    private int currentPageIndex = 0;

    public QueryController() {
        super(String.class);
    }

    public QueryController(QueryAvailableBalancePage queryABPage) {
        super(String.class);
        this.currentPageIndex = QUERY_AB;
        this.queryABPage = queryABPage;
    }

    public QueryController(QueryCurrentMonthPage queryCMPage) {
        super(String.class);
        this.currentPageIndex = QUERY_CM;
        this.queryCMPage = queryCMPage;
    }

    public QueryController(QueryHistoryPage queryHTPage) {
        super(String.class);
        this.currentPageIndex = QUERY_HT;
        this.queryHTPage = queryHTPage;
    }

    public QueryController(QueryPackageRemainPage queryPRPage) {
        super(String.class);
        this.currentPageIndex = QUERY_PR;
        this.queryPRPage = queryPRPage;
    }

    public QueryController(QueryPaymentPage queryPMPage) {
        super(String.class);
        this.currentPageIndex = QUERY_PM;
        this.queryPMPage = queryPMPage;
    }


    public void queryAB(String phoneNumber) {
//        18628196551
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_AVAILABLE_BALANCE).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    public void queryCM(String phoneNumber, String date) {
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><BillDate>" + date + "</BillDate></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_MONTH_BILL).setTelNo(phoneNumber).setSvcCont(svcCont).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    public void queryHT(String phoneNumber, String date) {
        queryCM(phoneNumber, date);
    }

    public void queryPR(String phoneNumber, String date) {
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_REMAIN).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    public void queryPM(String phoneNumber, Date date) {
        String svcCont = "<SvcCont><TelNo>" + phoneNumber + "</TelNo><BeginTime>" + DateUtils.getCurrentMonthBeginTime(date) + "</BeginTime><EndTime>" + DateUtils.getCurrentMonthEndTime(date) + "</EndTime></SvcCont>";
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_PAYMENT).setTelNo(phoneNumber).setSvcCont(svcCont).createXml();
//        AlertDialogUtils.showConfirmDiaLog(queryPMPage,postXml);
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onRequestFinish(String response) {
        if (response == null || "".equals(response)) {
            switch (currentPageIndex) {
                case QUERY_AB:
                    queryABPage.hideProgressDialog();
                    ToastUtils.showMessage(queryABPage, "错误，请稍候再试");
                    break;
                case QUERY_CM:
                    queryCMPage.hideProgressDialog();
                    ToastUtils.showMessage(queryCMPage, "错误，请稍候再试");
                    break;
                case QUERY_HT:
                    queryHTPage.hideProgressDialog();
                    ToastUtils.showMessage(queryHTPage, "错误，请稍候再试");
                    break;
                case QUERY_PR:
                    queryPRPage.hideProgressDialog();
                    ToastUtils.showMessage(queryPRPage, "错误，请稍候再试");
                    break;
                case QUERY_PM:
                    queryPMPage.hideProgressDialog();
                    ToastUtils.showMessage(queryPMPage, "错误，请稍候再试");
                    break;
            }
            return;
        }

        String respCode = SimpleXmlParser.getResponseCode(response);
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
//                AlertDialogUtils.showConfirmDiaLog(queryABPage,response);
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
                    String record = SimpleXmlParser.getElementsValue(svcCont, "Record").get(0);
                    String amount = SimpleXmlParser.getElementsValue(record, "Amount").get(0);
                    queryABPage.updateAvailableBalanceUI(amount);
                } else {
                    ToastUtils.showMessage(queryABPage, "出错了," + SimpleXmlParser.getResponseDes(response));
                }
                break;
            case QUERY_CM:
//                AlertDialogUtils.showConfirmDiaLog(queryCMPage, response);
                queryCMPage.hideProgressDialog();
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
//                    List<String> records = SimpleXmlParser.getElementsValue(svcCont, "DetailList");
//                    String totalFee = SimpleXmlParser.getElementsValue(response, "TotalFee").get(0);
//                    queryCMPage.updateCurrentMonthInfo(totalFee, svcCont);
                    queryCMPage.updateCurrentMonthInfo(svcCont);
                } else {
                    ToastUtils.showMessage(queryCMPage, "" + SimpleXmlParser.getResponseDes(response));
                }
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
//                    List<String> records = SimpleXmlParser.getElementsValue(svcCont, "DetailList");
//                    queryHTPage.updateListDate("WCDMA-66元A", records);
                    queryHTPage.updateListDate(svcCont);
                } else {
                    ToastUtils.showMessage(queryHTPage, "" + SimpleXmlParser.getResponseDes(response));
                }

                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
//                AlertDialogUtils.showConfirmDiaLog(queryPRPage,response);
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
                    List<String> records = SimpleXmlParser.getElementsValue(svcCont, "Record");
                    queryPRPage.updatePackageRemainInfo("WCDMA-66元A", records);//todo
                } else {
                    ToastUtils.showMessage(queryPRPage, "" + SimpleXmlParser.getResponseDes(response));
                }
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                if (ResponseCode.SUCCESS.equals(respCode)) {
                    String svcCont = SimpleXmlParser.getElementsValue(response, "SvcCont").get(0);
                    List<String> records = SimpleXmlParser.getElementsValue(svcCont, "Record");
                    queryPMPage.updatePaymentInfo(records);
                } else {
                    ToastUtils.showMessage(queryPMPage, "" + SimpleXmlParser.getResponseDes(response));
                }
                break;
        }
    }

    @Override
    public void onNetworkAbort() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，网络连接被中断.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，网络连接被中断.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，网络连接被中断.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，网络连接被中断.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，网络连接被中断.");
                break;
        }
    }

    @Override
    public void onNetworkError() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，网络连接错误.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，网络连接错误.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，网络连接错误.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，网络连接错误.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，网络连接错误.");
                break;
        }
    }


    @Override
    public void onResponseError() {
        switch (currentPageIndex) {
            case QUERY_AB:
                queryABPage.hideProgressDialog();
                ToastUtils.showMessage(queryABPage, "错误，返回结果错误.");
                break;
            case QUERY_CM:
                queryCMPage.hideProgressDialog();
                ToastUtils.showMessage(queryCMPage, "错误，返回结果错误.");
                break;
            case QUERY_HT:
                queryHTPage.hideProgressDialog();
                ToastUtils.showMessage(queryHTPage, "错误，返回结果错误.");
                break;
            case QUERY_PR:
                queryPRPage.hideProgressDialog();
                ToastUtils.showMessage(queryPRPage, "错误，返回结果错误.");
                break;
            case QUERY_PM:
                queryPMPage.hideProgressDialog();
                ToastUtils.showMessage(queryPMPage, "错误，返回结果错误.");
                break;
        }
    }
}
